package com.shengqi.xinnongren.work.ui.home.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.model.CartGoodsBean;
import com.shengqi.xinnongren.work.ui.home.model.CartMoneyBean;
import com.shengqi.xinnongren.work.ui.home.view.HomePage4View;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/7/25.
 */
public class HomePage4Presenter extends BasePresenter<HomePage4View> {
    /**
     * 获取购物车数据
     */
    public void getCartGoodsList() {
//        getView().showLoadingView();
        JSONObject jsonData = Constants.getAESJsonData(new HashMap<>(), UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCarData(requestBody), s -> {
//            getView().hideLoadingView();
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                List<CartGoodsBean> list = new Gson().fromJson(httpResult.data, new TypeToken<List<CartGoodsBean>>() {
                }.getType());
                getView().onGetCartGoodsList(list);
            } else if (httpResult.result == -1) {
                getView().onTokenInvalid();
            } else {
                getView().onRequestFailed(httpResult.message);
            }
        });
    }

    /**
     * 改变购物车规格数量
     */
    public void changeSpecNum(String skuId, int number) {
        Map<String, Object> map = new HashMap<>();
        map.put("skuId", skuId);
        map.put("number", number);
        JSONObject jsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().changeSpecNum(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                getView().onSpecificationNumChanged();
            } else {
                getView().onRequestFailed(httpResult.message);
            }
        });
    }

    /**
     * 从购物车里移除商品
     */
    public void removeCartGoods(String comId) {
        Map<String, Object> map = new HashMap<>();
        map.put("comId", comId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().removeCartGoods(requestBody), s -> {
            HttpRespond<String> httpRespond = Global.getHttpResult(s);
            if (httpRespond.result == 1) {
                getView().onRemoveCartGoodsSucceed();
            } else {
                getView().onRequestFailed(httpRespond.message);
            }
        });
    }

    /**
     * 计算购物车商品价格
     */
    public void calculateCartGoodsMoney(String comId) {
        Map<String, Object> map = new HashMap<>();
        map.put("comId", comId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().calculateMoney(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                CartMoneyBean cartMoneyBean = new Gson().fromJson(httpResult.data, CartMoneyBean.class);
                getView().onCartMoneyCalculated(cartMoneyBean);
            } else {
                getView().onRequestFailed(httpResult.message);
            }
        });
    }
}
